package com.ley.yikang.controller;

import com.ley.yikang.Annotation.UserLoginToken;
import com.ley.yikang.entity.*;
import com.ley.yikang.service.*;
import com.ley.yikang.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/community")
public class CommunityController {

    private final CommunityService communityService;
    private final DoctorService doctorService;
    private final ActionService actionService;
    private final CheckTypeService checkTypeService;
    private final ElderService elderService;
    private final ChildService childService;
    private final CheckTaskService checkTaskService;


    @Autowired
    public CommunityController(CommunityService communityService, DoctorService doctorService, ActionService actionService, CheckTypeService checkTypeService, ElderService elderService, ChildService childService, CheckTaskService checkTaskService) {
        this.communityService = communityService;
        this.doctorService = doctorService;
        this.actionService = actionService;
        this.checkTypeService = checkTypeService;
        this.elderService = elderService;
        this.childService = childService;
        this.checkTaskService = checkTaskService;
    }


    @UserLoginToken
    @PostMapping("/addCommunity")
    public ResultData<Boolean> addCommunity(@RequestBody Community community) {
        //判断用户是否存在
        Community community1 = communityService.findCommunityByName(community.getName());
        if (community1 == null) {
            communityService.addCommunity(community);
            System.out.println("添加社区成功！");
            return ResultData.success(true, "添加社区成功！");
        } else {
            System.out.println("添加社区失败！");
            return ResultData.fail("添加社区失败，该社区名称已存在！");
        }
    }

    @UserLoginToken
    @GetMapping("/findAllCommunity")
    public ResultData<List<Community>> findAll(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String address,
            @RequestParam(required = false) String phone
    ) {
        List<Community> communities = communityService.findAll(name, address, phone);
        System.out.println("查询所有社区");
        //如果查询的社区包括id为0的社区需要删除
        for (int i = 0; i < communities.size(); i++) {
            if (communities.get(i).getId() == 0) {
                communities.remove(i);
            }
        }
        return ResultData.success(communities, "查询成功！");
    }

    @UserLoginToken
    @GetMapping("/getCommunityById")
    public ResultData<Community> getCommunityById(int id) {
        Community community = communityService.findCommunityById(id);
        System.out.println("查询社区：" + id);
        return ResultData.success(community, "查询成功！");
    }

    @UserLoginToken
    @DeleteMapping("/delCommunity")
    public ResultData<Boolean> delete(@RequestBody Community community) {
        //判断社区是否存在
        if (communityService.findCommunityById(community.getId()) == null) {
            System.out.println("没有找到该社区！删除失败！");
            return ResultData.fail("没有找到该社区！删除失败！");
        }
        //判断如果该社区有老人、子女、医生用户则不能删除
        if (!elderService.getCommunityElder(community.getId()).isEmpty() || !childService.getCommunityChild(community.getId()).isEmpty() || !doctorService.getCommunityDoctor(community.getId()).isEmpty()) {
            System.out.println("该社区还有老人、子女、医生用户！删除失败！");
            return ResultData.fail("该社区还有老人、子女、医生用户！删除失败！");
        }
        actionService.delActionByCommunityId(community.getId());
        //删除社区
        communityService.delCommunity(community.getId());
        System.out.println("删除社区：" + community.getId());
        return ResultData.success(true, "删除社区成功！");
    }

    @UserLoginToken
    @PostMapping("/updateCommunity")
    public ResultData<Community> update(@RequestBody Community community) {
        //判断社区是否存在
        if (communityService.findCommunityById(community.getId()) == null) {
            System.out.println("没有找到该社区！修改失败！");
            return ResultData.fail("没有找到该社区！修改失败！");
        }
        //判断社区名称是否存在
        if (communityService.findCommunityByName(community.getName()) != null){
            System.out.println("该社区已存在！修改失败！");
            return ResultData.fail("该社区已存在！修改失败！");
        }
        communityService.updateCommunity(community);
        System.out.println("修改社区：" + community.getId());
        community = communityService.findCommunityById(community.getId());
        return ResultData.success(community, "修改社区成功！");
    }

    @UserLoginToken
    @GetMapping("/getCheckType")
    public ResultData<List<CheckType>> getCheckType() {
        return ResultData.success(checkTypeService.getCheckType(), "查询成功！");
    }

    @UserLoginToken
    @PostMapping("/addCheckType")
    public ResultData<Boolean> addCheckType(@RequestBody CheckType checkType) {
        //判断该检测类型是否存在
        CheckType checkType1 = checkTypeService.findCheckTypeByName(checkType.getName());
        if (checkType1 == null) {
            checkTypeService.addCheckType(checkType);
            System.out.println("添加检测类型成功！");
            return ResultData.success(true, "添加检测类型成功！");
        } else {
            System.out.println("添加检测类型失败！");
            return ResultData.fail("添加检测类型失败，该检测类型已存在！");
        }
    }

    @UserLoginToken
    @DeleteMapping("/delCheckType")
    public ResultData<Boolean> delCheckType(int id) {
        //判断该检测类型是否存在
        if (checkTypeService.findCheckTypeById(id) == null) {
            System.out.println("没有找到该检测类型！删除失败！");
            return ResultData.fail("没有找到该检测类型！删除失败！");
        }
        //如果有检查任务还在使用该检查类型，则不能删除
        if (checkTaskService.getAllCheckTask().stream().anyMatch(checkTask -> checkTask.getCheckTypeId() == id)) {
            System.out.println("该检测类型还有检查任务在使用！删除失败！");
            return ResultData.fail("该检测类型还有检查任务在使用！删除失败！");
        }
        checkTypeService.delCheckType(id);
        System.out.println("删除检测类型：" + id);
        return ResultData.success(true, "删除检测类型成功！");
    }

    @UserLoginToken
    @PostMapping("/updateCheckType")
    public ResultData<CheckType> updateCheckType(@RequestBody CheckType checkType) {
        //判断该检测类型是否存在
        if (checkTypeService.findCheckTypeById(checkType.getId()) == null) {
            System.out.println("没有找到该检测类型！修改失败！");
            return ResultData.fail("没有找到该检测类型！修改失败！");
        }
        checkTypeService.updateCheckType(checkType);
        System.out.println("修改检测类型：" + checkType.getId());
        checkType = checkTypeService.findCheckTypeById(checkType.getId());
        return ResultData.success(checkType, "修改检测类型成功！");
    }

    @UserLoginToken
    @PostMapping("/addAction")
    public ResultData<Boolean> addAction(@RequestBody Action action) {
        //判断社区是否存在
        if (communityService.findCommunityById(action.getCommunityId()) == null) {
            System.out.println("没有找到该社区！添加失败！");
            return ResultData.fail("没有找到该社区！添加失败！");
        }
        actionService.addAction(action);
        System.out.println("添加社区活动成功！");
        return ResultData.success(true, "添加社区活动成功！");
    }

    @UserLoginToken
    @GetMapping("/getActionByCommunityId")
    public ResultData<List<Action>> getActionByCommunityId(@RequestParam("communityId") int communityId) {
        List<Action> actions = actionService.getActionByCommunityId(communityId);
        System.out.println("查询社区所有活动");
        return ResultData.success(actions, "查询成功！");
    }

    @UserLoginToken
    @DeleteMapping("/delAction")
    public ResultData<Boolean> delAction(@RequestBody Action action) {
        //判断社区是否存在
        if (communityService.findCommunityById(action.getCommunityId()) == null) {
            System.out.println("没有找到该社区！删除失败！");
            return ResultData.fail("没有找到该社区！删除失败！");
        }
        //判断活动是否存在
        if (actionService.getActionById(action.getId()) == null) {
            System.out.println("没有找到该活动！删除失败！");
            return ResultData.fail("没有找到该活动！删除失败！");
        }
        actionService.delAction(action.getId());
        System.out.println("删除社区活动：" + action.getId());
        return ResultData.success(true, "删除社区活动成功！");
    }

    @UserLoginToken
    @PostMapping("/updateAction")
    public ResultData<Action> updateAction(@RequestBody Action action) {
        //判断社区是否存在
        if (communityService.findCommunityById(action.getCommunityId()) == null) {
            System.out.println("没有找到该社区！修改失败！");
            return ResultData.fail("没有找到该社区！修改失败！");
        }
        //判断活动是否存在
        if (actionService.getActionById(action.getId()) == null) {
            System.out.println("没有找到该活动！修改失败！");
            return ResultData.fail("没有找到该活动！修改失败！");
        }
        actionService.updateAction(action);
        System.out.println("修改社区活动：" + action.getId());
        action = actionService.getActionById(action.getId());
        return ResultData.success(action, "修改社区活动成功！");
    }

    @UserLoginToken
    @GetMapping("/getCommunityElder")
    public ResultData<List<Elder>> getCommunityElder(int communityId) {
        List<Elder> elders = elderService.getCommunityElder(communityId);
        System.out.println("查询社区所有老人");
        return ResultData.success(elders, "查询成功！");
    }

    @UserLoginToken
    @GetMapping("/getCommunityChild")
    public ResultData<List<Child>> getCommunityChild(int communityId) {
        List<Child> children = childService.getCommunityChild(communityId);
        System.out.println("查询社区所有儿童");
        return ResultData.success(children, "查询成功！");
    }

    @UserLoginToken
    @GetMapping("/getCommunityDoctor")
    public ResultData<List<Doctor>> getCommunityDoctor(int communityId) {
        List<Doctor> doctors = doctorService.getCommunityDoctor(communityId);
        System.out.println("查询社区所有医生");
        return ResultData.success(doctors, "查询成功！");
    }
}
